package com.ffyc.cms.controller;
import com.ffyc.cms.common.CommonResult;
import com.ffyc.cms.model.Admin;
import com.ffyc.cms.model.Advertising;
import com.ffyc.cms.service.AdvertisingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * 广告显示控制层
 */
@RestController
@RequestMapping(path = "advertising")
public class AdvertisingController {
    @Autowired
    AdvertisingService advertisingService;

    @RequestMapping(path ="getadvertisingList" )
    public CommonResult getadvertisingList(){
        CommonResult commonResult=null;
        try{
            List<Advertising> advertisingList=advertisingService.getadvertisingList();
            if (advertisingList==null){
                commonResult=new CommonResult(201,"广告信息查询为空",null);
            }else{
                commonResult=new CommonResult(200,"广告信息查询成功",advertisingList);
            }
        }catch (Exception e){
            e.printStackTrace();
            commonResult=new CommonResult(500,"广告信息查询失败",null);
        }
        return commonResult;
    }
    @RequestMapping(path ="getadvertisingListById" )
    public CommonResult getadvertisingListById(Integer id){
        CommonResult commonResult=null;
        try{
            Advertising advertising=advertisingService.getadvertisingListById(id);

            commonResult=new CommonResult(200,"广告信息查询成功",advertising);
        }catch (Exception e){
            e.printStackTrace();
            commonResult=new CommonResult(500,"广告信息查询失败",null);
        }
        return commonResult;
    }

    @PostMapping(path = "/fileUpload")
    public CommonResult fileUpload(@RequestParam("fileName") CommonsMultipartFile file , HttpSession session){
        CommonResult commonResult = null;
        File f0 = new File("C:\\userImg\\advertisingphoto");
        if (!f0.exists()){
            f0.mkdir();
        }
        String oldFileName = file.getOriginalFilename();
        File f1 = new File(f0,oldFileName);//输出路径
        try {
            file.transferTo(f1);
            commonResult = new CommonResult(200,"上传成功",oldFileName);
        } catch (IOException e) {
            e.printStackTrace();
            commonResult = new CommonResult(500,"上传失败",null);
        }
        return commonResult;
    }
    @PostMapping(path = "/updateadv")
    public CommonResult updateAdv(Advertising advertising,HttpSession session){
        CommonResult commonResult = null;
        try{
            Admin admin = (Admin)session.getAttribute("admin");
            advertisingService.updateAdv(advertising,admin);
            commonResult = new CommonResult(200,"保存成功",null);
            return commonResult;
        }catch (Exception e){
            e.printStackTrace();
            commonResult = new CommonResult(500,"服务器忙，请稍后重试",null);
            return commonResult;
        }
    }
}
